package broadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.Parcelable;
import android.util.Log;

import com.example.administrator.newxlmedical.EnterActivity;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.List;

import beans.TokenBean;
import constants.Constants;
import constants.HandlerConstant;
import customhandler.MyHandler;
import netframe.GetRetrofit;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;
import singleton.SingletonClass;
import utils.ActivityUtil;
import utils.NetUtil;
import utils.ToastUtil;

/**
 * Created by Administrator on 2018/6/22.
 */

public class NetworkConnectChangedReceiver extends BroadcastReceiver {
    private MyHandler allHandler;
    private String getConnectionType(int type) {
        String connType = "";
        if (type == ConnectivityManager.TYPE_MOBILE) {
            connType = "数据网络";
        } else if (type == ConnectivityManager.TYPE_WIFI) {
            connType = "WIFI网络";
        }
            return connType;
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        allHandler = SingletonClass.getSingleton(MyHandler.class);

        if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
            int netWorkState = NetUtil.getNetWorkState(context);
            // 接口回调传过去状态的类型
            Log.e("Network", "onReceive: " + netWorkState );
            if (netWorkState != Constants.NETWORK_NONE){
//                GetRetrofit.getInstance().getToken("select ID from Token where TerminalInfoId = 0")
//                        .subscribeOn(Schedulers.io())
//                        .observeOn(AndroidSchedulers.mainThread())
//                        .subscribe(new Action1<List<TokenBean>>() {
//                            @Override
//                            public void call(List<TokenBean> tokenBeans) {
//
//                            }
//                        }, new Action1<Throwable>() {
//                            @Override
//                            public void call(Throwable throwable) {
//
//                            }
//                        });
                allHandler.sendEmptyMessage(HandlerConstant.NETWORK_REFRESH);

            }else {
                allHandler.sendEmptyMessage(HandlerConstant.NETWORK_NONE);
            }

        }
//
//        if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(intent.getAction())) {
//            Parcelable parcelableExtra = intent
//                    .getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
//            if (null != parcelableExtra) {
//                // 获取联网状态的NetWorkInfo对象
//                NetworkInfo networkInfo = (NetworkInfo) parcelableExtra;
//                // 获取的State对象则代表着连接成功与否等状态
//                NetworkInfo.State state = networkInfo.getState();
//                // 判断网络是否已经连接
//                boolean isConnected = state == NetworkInfo.State.SUSPENDED;
//                Log.e("Network", "isConnected:" + isConnected);
//
//            }
//        }

//        if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(intent.getAction())) {// 监听wifi的打开与关闭，与wifi的连接无关
//            int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);
//            Log.e("TAG", "wifiState:" + wifiState);
//            switch (wifiState) {
//                case WifiManager.WIFI_STATE_DISABLED:
//                    break;
//                case WifiManager.WIFI_STATE_DISABLING:
//                    break;
//            }
//        }
//
//        // 监听wifi的连接状态即是否连上了一个有效无线路由
//        if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(intent.getAction())) {
//            Parcelable parcelableExtra = intent
//                    .getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
//            if (null != parcelableExtra) {
//                // 获取联网状态的NetWorkInfo对象
//                NetworkInfo networkInfo = (NetworkInfo) parcelableExtra;
//                //获取的State对象则代表着连接成功与否等状态
//                NetworkInfo.State state = networkInfo.getState();
//                //判断网络是否已经连接
//                boolean isConnected = state == NetworkInfo.State.CONNECTED;
//                Log.e("TAG", "isConnected:" + isConnected);
//                if (isConnected) {
//                } else {
//                }
//            }
//        }
//        if (context != null) {
//            ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
//            NetworkInfo mInternetNetWorkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_ETHERNET);
//            boolean hasInternet = !isNullObject(mInternetNetWorkInfo) && mInternetNetWorkInfo.isConnected() && mInternetNetWorkInfo.isAvailable();
//            if (hasInternet && mInternetNetWorkInfo.isAvailable()){
//                Log.e("TAG", "onReceive: true" );
//                allHandler.sendEmptyMessage(HandlerConstant.NETWORK_REFRESH);
//                ToastUtil.showToast(context,"网络已连接!");
//            }else {
//                Log.e("TAG", "onReceive: false" );
//            }
//        }
//        // 监听网络连接，包括wifi和移动数据的打开和关闭,以及连接上可用的连接都会接到监听
//        if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
//            //获取联网状态的NetworkInfo对象
//            NetworkInfo info = intent
//                    .getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
//
//            if (info != null) {
//                //如果当前的网络连接成功并且网络连接可用
//                if (NetworkInfo.State.CONNECTED == info.getState() && info.isAvailable()) {
//                    if (info.getType() == ConnectivityManager.TYPE_WIFI
//                            || info.getType() == ConnectivityManager.TYPE_MOBILE) {
//                        allHandler.sendEmptyMessage(HandlerConstant.NETWORK_REFRESH);
//                        Log.e("TAG", getConnectionType(info.getType()) + "连上");
//                        ToastUtil.showToast(context,"网络已连接!");
//                    }
//                } else {
//                    Log.e("TAG", getConnectionType(info.getType()) + "网络断开");
//                    ToastUtil.showToast(context,"网络已断开!");
//                    allHandler.sendEmptyMessage(HandlerConstant.NET_ERROR);
//                }
//            }
//        }

    }
    public boolean isNullObject (Object object) {

        if (object == null) {
            return true;
        }

        return false;
    }
}
